
#include "modules/mod_sampler2d.frag"

uniform sampler2D colorTex0_;
uniform sampler2D depthTex0_;
uniform TextureParameters textureParameters0_;
uniform sampler2D colorTex1_;
uniform sampler2D depthTex1_;
uniform TextureParameters textureParameters1_;

uniform int compositingMode_;
uniform int kernelSize_;

uniform int patternWidth_;
uniform int offsetX_;
uniform int offsetY_;

uniform float overlayThreshold_;

vec4 calcGaussian(sampler2D coltex, TextureParameters texparams, vec2 coords){
	float xmin = max(0, coords.x - kernelSize_);
	float xmax = coords.x + kernelSize_;
	float ymin = max(0, coords.y - kernelSize_);
	float ymax = coords.y + kernelSize_;
	vec4 value = vec4(0.0,0.0,0.0,0.0);
	for(float x=xmin; x<=xmax; x+=1.0){
		for(float y=ymin; y<=ymax; y+=1.0){
			value += textureLookup2Dscreen(coltex, texparams, vec2(x,y));
		}
	}
	value /= ((xmax+1.0-xmin)*(ymax+1.0-ymin));
	return value;
}

void main() {
    vec2 fragCoord = gl_FragCoord.xy;

    // fetch input textures
    vec4 color0 = textureLookup2Dscreen(colorTex0_, textureParameters0_, fragCoord);
    float depth0 = textureLookup2Dscreen(depthTex0_, textureParameters0_, fragCoord).z;
    vec4 color1 = textureLookup2Dscreen(colorTex1_, textureParameters1_, fragCoord);
    float depth1 = textureLookup2Dscreen(depthTex1_, textureParameters1_, fragCoord).z;

    vec4 fragColor = vec4(0.0);
    float fragDepth = 1.0;

    #if defined(GAUSSIAN_1)
		fragColor.rgba = calcGaussian(colorTex0_, textureParameters0_, fragCoord);
		fragDepth = depth0;
    #elif defined(GAUSSIAN_2)
		fragColor.rgba = calcGaussian(colorTex1_, textureParameters1_, fragCoord);
		fragDepth = depth1;
	#elif defined(GRADIENT_1)
		vec2 pos = vec2(fragCoord.x-1.0, fragCoord.y-1.0);
		vec4 col0 = calcGaussian(colorTex0_, textureParameters0_, pos);
		vec4 col1 = calcGaussian(colorTex0_, textureParameters0_, fragCoord);
		fragColor.rgba = col0 - col1;
	#elif defined(GRADIENT_2)
		vec2 pos = vec2(fragCoord.x-1.0, fragCoord.y-1.0);
		vec4 col0 = calcGaussian(colorTex1_, textureParameters1_, pos);
		vec4 col1 = calcGaussian(colorTex1_, textureParameters1_, fragCoord);
		fragColor.rgba = col0 - col1;
	#elif defined(BUMPMAP_1)
		vec2 pos = vec2(fragCoord.x-1.0, fragCoord.y-1.0);
		vec4 col0 = calcGaussian(colorTex0_, textureParameters0_, pos);
		vec4 col1 = calcGaussian(colorTex0_, textureParameters0_, fragCoord);
		vec4 bumpm = col0 - col1;
		bumpm = vec4(bumpm.r+1.0, bumpm.g+1.0, bumpm.b+1.0, bumpm.a+1.0);
		fragColor.rgba = color1 * bumpm;
	#elif defined(BUMPMAP_2)
		vec2 pos = vec2(fragCoord.x-1.0, fragCoord.y-1.0);
		vec4 col0 = calcGaussian(colorTex1_, textureParameters1_, pos);
		vec4 col1 = calcGaussian(colorTex1_, textureParameters1_, fragCoord);
		vec4 bumpm = col0 - col1;
		bumpm = vec4(bumpm.r+1.0, bumpm.g+1.0, bumpm.b+1.0, bumpm.a+1.0);
		fragColor.rgba = color0 * bumpm;
	#elif defined(MSK_CHECKER)
		bool xfold = int((fragCoord.x + offsetX_) / patternWidth_)%2 == 1;
		bool yfold = int((fragCoord.y + offsetY_) / patternWidth_)%2 == 1;
		if( (xfold && yfold) || (!xfold && !yfold) ){
			fragColor.rgba = color0;
		}else{
			fragColor.rgba = color1;
		}
	#elif defined(OVERLAY_1)
		float intensity = max(color0.r, max(color0.g, color0.b));
		if(intensity >= overlayThreshold_){
			fragColor.rgba = color0;
		}else{
			fragColor.rgba = color1;
		}
    #endif

    FragData0 = fragColor;
    gl_FragDepth = fragDepth;
}
